From 6f0c1093ac65064c9c19a4137ff897137f74c694 Mon Sep 17 00:00:00 2001
From: Ilion Beyst <ilion.beyst@gmail.com>
Date: Sat, 6 Aug 2022 16:12:00 +0200
Subject: extend api_client for loading match data

---
 web/pw-server/src/routes/matches/[match_id].svelte | 40 +++++++++-------------
 1 file changed, 16 insertions(+), 24 deletions(-)

(limited to 'web/pw-server/src/routes/matches/[match_id].svelte')

diff --git a/web/pw-server/src/routes/matches/[match_id].svelte b/web/pw-server/src/routes/matches/[match_id].svelte
index 2c0a3fa..7c1507c 100644
--- a/web/pw-server/src/routes/matches/[match_id].svelte
+++ b/web/pw-server/src/routes/matches/[match_id].svelte
@@ -1,33 +1,25 @@
 <script lang="ts" context="module">
-  function fetchJson(url: string): Promise<Response> {
-    return fetch(url, {
-      headers: {
-        "Content-Type": "application/json",
-      },
-    });
-  }
-
-  export async function load({ params }) {
-    // TODO: handle failure cases better
-    const matchId = params["match_id"];
-    const matchDataResponse = await fetchJson(`/api/matches/${matchId}`);
-    if (!matchDataResponse.ok) {
-    }
-    const matchLogResponse = await fetchJson(`/api/matches/${matchId}/log`);
-
-    if (matchDataResponse.ok && matchLogResponse.ok) {
+  import { ApiClient } from "$lib/api_client";
+  export async function load({ params, fetch }) {
+    try {
+      const matchId = params["match_id"];
+      const apiClient = new ApiClient(fetch);
+      const [matchData, matchLog] = await Promise.all([
+        apiClient.get(`/api/matches/${matchId}`),
+        apiClient.getText(`/api/matches/${matchId}/log`),
+      ]);
       return {
         props: {
-          matchData: await matchDataResponse.json(),
-          matchLog: await matchLogResponse.text(),
+          matchData: matchData,
+          matchLog: matchLog,
         },
       };
+    } catch (error) {
+      return {
+        status: error.status,
+        error: error,
+      };
     }
-
-    return {
-      status: matchDataResponse.status,
-      error: new Error("failed to load match"),
-    };
   }
 </script>
 
-- 
cgit v1.2.3

